package com.lktx.auto.browser;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.lktx.auto.bean.Resume;
import com.microsoft.playwright.*;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class BrowserSpider {

    private  static BrowserContext browserContext;

    public static void initBrowser(){
        Playwright playwright = Playwright.create();
        BrowserType browserType = playwright.chromium();
        BrowserType.LaunchPersistentContextOptions launchOptions = new BrowserType.LaunchPersistentContextOptions().setHeadless(false);
        String contextDir = Paths.get("").toAbsolutePath()+ File.separator+"cache";
        browserContext = browserType.launchPersistentContext(Path.of(contextDir), launchOptions);
    }


    public static String getResume(String keyword, int pageNum){
        List<Resume> list = new ArrayList<>();
        Page page = browserContext.newPage();
        page.onResponse(response -> {
            if (response.url().contains("api/talent/search/list")){
                String text = response.text();
                JSONObject obj = JSONUtil.parseObj(text);
                JSONArray jsonArray = obj.getJSONObject("data").getJSONArray("list");
                for (int i = 1; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Resume resume = new Resume();
                    resume.setUserName(jsonObject.getStr("userName"));
                    resume.setAvatar(jsonObject.getStr("avatar"));
                    resume.setGender(jsonObject.getStr("gender"));
                    resume.setAge(jsonObject.getStr("age"));
                    resume.setWorkYears(jsonObject.getStr("workYears"));
                    resume.setDisplayTags(jsonObject.getStr("displayTags"));
                    resume.setDesiredSalary(jsonObject.getStr("desiredSalary"));
                    resume.setWorkExperiences(jsonObject.getStr("workExperiences"));
                    resume.setEducationExperiences(jsonObject.getStr("educationExperiences"));
                    resume.setNewUserActiveTag(jsonObject.getStr("newUserActiveTag"));
                    resume.setResumeNumber(jsonObject.getStr("resumeNumber"));
                    resume.setResumeK(jsonObject.getStr("resumeK"));
                    resume.setResumeT(jsonObject.getStr("resumeT"));
                    list.add(resume);
                }
            }
        });
        page.navigate("https://rd6.zhaopin.com/app/search");
        page.waitForTimeout(500);
        page.locator("input[class='keyword-input-tag-item-input__input']").fill(keyword);
        page.waitForTimeout(100);
        page.locator("button[class='keyword-input-tag__btn km-button km-control km-ripple-off km-button--primary km-button--filled']").click();
        page.waitForTimeout(1000);
        for (int i = 0; i < pageNum; i++) {
            Locator locator2 = page.locator("a[class='km-pagination__pager km-pagination__pager--arrow']");
            locator2.last().click();
            page.waitForTimeout(1000);
        }
        page.close();
        return JSONUtil.toJsonStr(list);
    }


    public static void main(String[] args) {
        BrowserSpider.initBrowser();
        BrowserSpider.getResume("人工智能",5);

    }


}
